function findByName(name) {
	return document.getElementsByName(name);
}
function $2(name) {
	return window.dialogArguments.document.getElementsByName(name);
}
function getUpFields(name) {
	return up_doc.$("[validate*='name:\"" + name + "\"']");
}
function getUpFieldsByFieldval(name) {
	return up_doc.$("[fieldval*='displayName:\"" + name + "\"']");
}
function getUpFieldValue(upfield) {
	if (upfield.is("input")) {
		alert("is input");
		return upfield.val() ? upfield.val() : "";
	} else if (upfield.is("span")) {
		alert("is span");
		return upfield.text() ? upfield.text() : "";
	}
}
function nextSelect(dom) {
	var parent = dom.parentNode;
}
var aptitudeClassMap = {
	aptitudeClassSelect : [ "my:资质类别", "my:资质类别PK" ],
	// offer : "my:是否提供",
	startTime : "my:开始时间",
	endTime : "my:到期时间",
	inspection : "my:是否年检",
	record : "my:资质复审依据及记录",
	remark : "my:备注"
};
var repeatOperator;
var aptitudeSelectNameArray = [ 'aptitudeClassSelect', 'aptitudeClassSelect1',
		'aptitudeClassSelect2', 'aptitudeClassSelect3' ];

/**
 * 重置，将资质管理的行数为1行，并且字段置空
 */
function resetAptitude() {
	var length = findByName("aptitudeClassSelect").length;
	for ( var i = 1; i < length; i++) {
		findByName("aptitudeClassSelect")[0].parentNode.parentNode.parentNode
				.removeChild(findByName("aptitudeClassSelect")[0].parentNode.parentNode)
	}
	for ( var key in aptitudeClassMap) {
		if (findByName(key)[0])
			findByName(key)[0].value = "";
	}
	if (repeatOperator) {
		repeatOperator.hideOperateDiv1();
	}
	hideAptitude();
	hideAptitudeSelect(findByName("aptitudeClassSelect")[0]);
}
/**
 * 供应商资质管理字段不隐藏
 */
function showAptitude() {
	findByName("aptitudeClassSelect")[0].parentNode.parentNode.style.display = "";
}

/**
 * 隐藏供应商资质管理字段
 */
function hideAptitude() {
	findByName("aptitudeClassSelect")[0].parentNode.parentNode.style.display = "none";
}

/**
 * 将资质管理的输入行注册成可增加或减少的行
 */
function registerRepeatTR() {
	repeatOperator = new RepeateTableOperator();
	repeatOperator
			.addListeners(findByName("aptitudeClassSelect")[0].parentNode.parentNode);
	repeatOperator.setAfterAddTrOperate(function(cloneTr) {
		var selectNodes = cloneTr.getElementsByTagName("select");
		for ( var i = selectNodes.length - 1; i > 0; i--) {
			if (selectNodes[i].name != "inspection") {
				selectNodes[i].options.length = 0;
				selectNodes[i].style.display = "none";
			}
		}
		selectNodes[0].value = "";
	});
}

/**
 * 读取所选的供应商资质类别
 * 
 * @param {}
 *            index
 * @param {}
 *            aptitudeClassPK
 */
function readAptitudeClassValue(index, aptitudeClassPK) {
	if (aptitudeClassPK && aptitudeClassPK != null) {
		var selectValuePathArray = [];
		selectValuePathArray.push(aptitudeClassPK);
		var hasnext = true;
		while (hasnext) {
			hasnext = false;
			for ( var i = 0; i < aptitudeClassJSON.length; i++) {
				if (aptitudeClassJSON[i].pk_aptclas == aptitudeClassPK
						&& aptitudeClassJSON[i].parent) {
					aptitudeClassPK = aptitudeClassJSON[i].parent;
					selectValuePathArray.push(aptitudeClassPK);
					hasnext = true;
					break;
				}
			}
		}
		for ( var i = selectValuePathArray.length - 1; i >= 0; i--) {
			findByName(aptitudeSelectNameArray[selectValuePathArray.length - 1
					- i])[index].value = selectValuePathArray[i];
			aptitudeClassSelectChange(findByName(aptitudeSelectNameArray[selectValuePathArray.length
					- 1 - i])[index]);
		}
	}
}
function createOption(msg,key){
	var op = document.createElement("option");
	op.value = key;
	op.text = msg;
	return op;
}

/**
 * 供应商资质管理初始化
 */
function initAptitude() {
	findByName("aptitudeClassSelect")[0].options.add(createOption("", ""));
	for ( var i = 0; i < aptitudeClassJSON.length; i++) {
		if (!aptitudeClassJSON[i].parent || aptitudeClassJSON[i].parent == "")
			findByName("aptitudeClassSelect")[0].options
					.add(createOption(aptitudeClassJSON[i].name,
							aptitudeClassJSON[i].pk_aptclas));
	}
	registerRepeatTR();
	if (findByName("providerIsExist")[0]
			&& findByName("providerIsExist")[0].checked == true
			|| getUpFields("my:信息是否改变").length > 0
			&& getUpFields("my:信息是否改变").find(":radio").eq(0).attr('val4cal') == 1
			|| getUpFields("my:供应商编码").length > 0
			&& getUpFieldValue(getUpFields("my:供应商编码").eq(0)) != "") {
		showAptitude();
		var length = getUpFields(aptitudeClassMap.aptitudeClassSelect[0]).length;
		repeatOperator
				.addEmptyTR(
						findByName("aptitudeClassSelect")[0].parentNode.parentNode,
						(getUpFields(aptitudeClassMap.aptitudeClassSelect[0]).length - 1));

		for ( var i = 0; i < length; i++) {
			for ( var key in aptitudeClassMap) {
				if (findByName(key)[i])
					if (key == "aptitudeClassSelect") {
						readAptitudeClassValue(i, getUpFieldValue(getUpFields(
								aptitudeClassMap[key][1]).eq(i)));
					} else {
						findByName(key)[i].value = getUpFieldValue(getUpFields(
								aptitudeClassMap[key][1]).eq(i));
					}
			}
		}
	}
}

function getAptitudeClassSelectName(index) {
	for ( var j = aptitudeSelectNameArray.length - 1; j > 0; j--) {
		if (findByName(aptitudeSelectNameArray[j])[index].value
				&& findByName(aptitudeSelectNameArray[j])[index].value != "") {
			return aptitudeSelectNameArray[j];
		}
	}
	return "aptitudeClassSelect";
}

/**
 * 写入到表单的供应商资质管理
 */
function writeForm_Apptitude() {
	var length = findByName("aptitudeClassSelect").length;
	for ( var i = 0; i < length; i++) {
		for ( var j = i + 1; j < length; j++) {
			if (findByName(getAptitudeClassSelectName(i))[i].value == findByName(getAptitudeClassSelectName(j))[j].value) {
				alert("不允许录入相同的供应商资质");
				return false;
			}
		}
	}
	var count = length - $2(aptitudeClassMap.aptitudeClassSelect[0]).length;
	// alert(length);
	// alert($2(aptitudeClassMap.aptitudeClassSelect[0]).length);
	// alert(count);
	for ( var i = 0; i < Math.abs(count); i++) {
		getParentTR($2(aptitudeClassMap.aptitudeClassSelect[0])[0]).id = "oper";
		if (count > 0) {
			window.dialogArguments.addEmpty();
			getParentTR($2(aptitudeClassMap.aptitudeClassSelect[0])[0]).id = "";
		} else
			window.dialogArguments.del();
	}
	for ( var i = 0; i < length; i++) {
		for ( var key in aptitudeClassMap) {
			if (findByName(key)[i]) {
				if (key == "aptitudeClassSelect") {
					var selectName = getAptitudeClassSelectName(i);
					$2(aptitudeClassMap[key][1])[i].value = findByName(selectName)[i].value;
					$2(aptitudeClassMap[key][0])[i].value = findByName(selectName)[i].options[findByName(selectName)[i].selectedIndex].text;
				} else {
					$2(aptitudeClassMap[key])[i].value = findByName(key)[i].value;
				}
			}
		}
	}
	return true;
}

function insertAfter(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		// 如果最后的节点是目标元素，则直接添加。因为默认是最后
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement, targetElement.nextSibling);
		// 如果不是，则插入在目标元素的下一个兄弟节点 的前面。也就是目标元素的后面
	}
}

function hasChildAptitude(selectNode) {
	if (selectNode.value && selectNode.value != "") {
		for ( var i = 0; i < aptitudeClassJSON.length; i++) {
			if (aptitudeClassJSON[i].parent == selectNode.value) {
				return true;
			}
		}
	}
	return false;
}

function arrayIndexof(array, str) {
	for ( var i = 0; i < array.length; i++) {
		if (array[i] === str) {
			return i;
		}
	}
	return -1;
}
/**
 * 隐藏所选下拉列表的下级下拉列表
 * 
 * @param {}
 *            selectNode
 */
function hideAptitudeSelect(selectNode) {
	var parent = selectNode.parentNode;
	if (parent.lastChild != selectNode) {
		var nextNode = selectNode.nextSibling;
		while (parent.lastChild != nextNode) {
			if (nextNode.tagName && nextNode.tagName.toUpperCase() == "SELECT") {
				nextNode.style.display = "none";
				nextNode.options.length = 0;
			}
			nextNode = nextNode.nextSibling;
		}
		if (nextNode.tagName && nextNode.tagName.toUpperCase() == "SELECT") {
			nextNode.style.display = "none";
			nextNode.options.length = 0;
		}
	}
}
/**
 * 为下一个下拉列表添加列表项
 * 
 * @param {}
 *            selectNode
 */
function addNextSelectOptions(selectNode) {
	var parent = selectNode.parentNode;
	if (parent.lastChild != selectNode) {
		var nextNode = selectNode.nextSibling;
		var nextSelect = null;
		while (parent.lastChild != nextNode) {
			if (nextNode.tagName.toUpperCase() == "SELECT") {
				nextSelect = nextNode;
				break;
			}
			nextNode = nextNode.nextSibling;
		}
		if (nextNode.tagName.toUpperCase() == "SELECT") {
			nextSelect = nextNode;
		}

		if (nextSelect) {
			nextSelect.style.display = "";
			nextSelect.options.add(createOption("", ""));
			for ( var i = 0; i < aptitudeClassJSON.length; i++) {
				if (aptitudeClassJSON[i].parent == selectNode.value)
					nextSelect.options.add(createOption(
							aptitudeClassJSON[i].name,
							aptitudeClassJSON[i].pk_aptclas));
			}
		}
	}
}

/**
 * 当供应商的资质类别变更时
 */
function aptitudeClassSelectChange(selectNode) {
	hideAptitudeSelect(selectNode);
	if (hasChildAptitude(selectNode)) {
		addNextSelectOptions(selectNode);
	}
}
